package com.zhoug.zmui.badge;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.zhoug.common.utils.AppUtils;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.StringUtils;
import com.zhoug.zmui.R;
import com.zhoug.zmui.beans.consts.Position;
import com.zhoug.zmui.round.ZMUIRoundTextView;
import com.zhoug.zmui.utils.ZMUIUtils;

import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.core.content.ContextCompat;

/**
 * 角标容器
 *R.attr.zmui_style_ZMUIBadgeFrameLayout
 * R.style.zmui_style_ZMUIBadgeFrameLayout
 * @Author 35574
 * @Date 2020/9/9
 * @Description
 */
public class ZMUIBadgeFrameLayout extends FrameLayout {
    private static final String TAG = ">>>ZMUIBadgeFrameLayout";
    private static final boolean DEBUG = false;
    private static final int DEFAULT_POSITION = Position.POSITION_TOP_RIGHT;

    private View mTarget;//需要添加角标的View
    private int badgePosition;//角标位置
    private TextView mBadgeView;
    private String badgeText;//角标
    private Drawable badgeBackground;//角标背景
    private int badgePadding;//角标padding
    private int badgeMarginV;//角标距离边界的垂直距离
    private int badgeMarginH;//角标距离边界的水平距离
    private int badgeTextColor;//角标文字颜色
    private float badgeTextSize;//角标文字大小
    private boolean isRound = false;

    public ZMUIBadgeFrameLayout(@NonNull Context context) {
        this(context, null);
    }

    public ZMUIBadgeFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, R.attr.zmui_style_ZMUIBadgeFrameLayout);
    }

    public ZMUIBadgeFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        parseAttrs(context, attrs, defStyleAttr, R.style.zmui_style_ZMUIBadgeFrameLayout);
    }


    private void parseAttrs(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZMUIBadgeFrameLayout, defStyleAttr, defStyleRes);
        badgePosition = a.getInt(R.styleable.ZMUIBadgeFrameLayout_zmui_position, DEFAULT_POSITION);
        badgeText = a.getString(R.styleable.ZMUIBadgeFrameLayout_zmui_badgeText);
        badgeTextColor = a.getColor(R.styleable.ZMUIBadgeFrameLayout_zmui_badgeTextColor, Color.WHITE);
        badgeTextSize = a.getDimension(R.styleable.ZMUIBadgeFrameLayout_zmui_badgeTextSize, context.getResources().getDimension(R.dimen.zmui_textSize_12));
        badgeBackground = a.getDrawable(R.styleable.ZMUIBadgeFrameLayout_zmui_badgeBackground);
        isRound = a.getBoolean(R.styleable.ZMUIBadgeFrameLayout_zmui_isRound, true);

        badgePadding = a.getDimensionPixelOffset(R.styleable.ZMUIBadgeFrameLayout_zmui_badgePadding, 0);
        badgeMarginV = a.getDimensionPixelOffset(R.styleable.ZMUIBadgeFrameLayout_zmui_badgeMarginV, 0);
        badgeMarginH = a.getDimensionPixelOffset(R.styleable.ZMUIBadgeFrameLayout_zmui_badgeMarginH, 0);


        a.recycle();
        if (badgeBackground == null) {
            badgeBackground = getDefaultBadgeBackground();
        }
    }

    private void init() {
        int childCount = getChildCount();
        if (DEBUG) {
            LogUtils.d(TAG, "init:childCount=" + childCount);
        }
        if (childCount > 1) {
            throw new RuntimeException("子View只能有一个");
        }
        mBadgeView = new TextView(getContext());
        //布局位置
        FrameLayout.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        switch (badgePosition) {
            case Position.POSITION_BOTTOM_LEFT:
                layoutParams.gravity = Gravity.BOTTOM | Gravity.START;
                layoutParams.bottomMargin = badgeMarginV;
                layoutParams.leftMargin = badgeMarginH;
                break;
            case Position.POSITION_BOTTOM_RIGHT:
                layoutParams.gravity = Gravity.BOTTOM | Gravity.END;
                layoutParams.bottomMargin = badgeMarginV;
                layoutParams.rightMargin = badgeMarginH;
                break;
            case Position.POSITION_TOP_LEFT:
                layoutParams.gravity = Gravity.TOP | Gravity.START;
                layoutParams.leftMargin = badgeMarginH;
                layoutParams.topMargin = badgeMarginV;
                break;
            case Position.POSITION_CENTER:
                layoutParams.gravity = Gravity.CENTER;
                layoutParams.bottomMargin = badgeMarginV;
                layoutParams.rightMargin = badgeMarginH;
                layoutParams.leftMargin = badgeMarginH;
                layoutParams.topMargin = badgeMarginV;
                break;
            default:
                layoutParams.gravity = Gravity.TOP | Gravity.END;
                layoutParams.topMargin = badgeMarginV;
                layoutParams.rightMargin = badgeMarginH;
                break;
        }
        mBadgeView.setLayoutParams(layoutParams);
        addView(mBadgeView, getChildCount());

        mBadgeView.setGravity(Gravity.CENTER);
        ZMUIUtils.setTextSize(mBadgeView, badgeTextSize);
        ZMUIUtils.setTextColor(mBadgeView, badgeTextColor);
        mBadgeView.setBackground(badgeBackground);
        mBadgeView.setPadding(badgePadding, badgePadding, badgePadding, badgePadding);
        setBadge(badgeText);

    }

    @Override
    protected void onFinishInflate() {
        init();
        super.onFinishInflate();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    /**
     * 显示角标
     */
    public void show() {
        if (mBadgeView.getVisibility() != VISIBLE) {
            mBadgeView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏角标
     */
    public void hide() {
        if (mBadgeView.getVisibility() == VISIBLE) {
            mBadgeView.setVisibility(View.GONE);
        }
    }

    /**
     * 默认的角标背景
     *
     * @return
     */
    private Drawable getDefaultBadgeBackground() {
      /*  int r = ResourceUtils.dpTopx(DEFAULT_CORNER_RADIUS_DIP);
        float[] outerR = new float[] {r, r, r, r, r, r, r, r};
        RoundRectShape shape = new RoundRectShape(outerR,null,null);
        ShapeDrawable shapeDrawable = new ShapeDrawable(shape);
        shapeDrawable.getPaint().setColor(Color.RED);
        return shapeDrawable;*/
        return ContextCompat.getDrawable(getContext(), R.drawable.zmui_badge_bg);
    }

    /**
     * 设置标记
     *
     * @param badge
     */
    public void setBadge(int badge) {
        badgeText = String.valueOf(badge);
        mBadgeView.setText(badgeText);
        measureBadgeSize();
        if (badge > 0) {
            show();
        } else {
            hide();
        }
    }

    /**
     * 设置标记
     *
     * @param badgeText
     */
    public void setBadge(String badgeText) {
        this.badgeText = badgeText;
        if (StringUtils.isNullOrEmpty(badgeText)) {
            mBadgeView.setText(null);
            hide();
        } else {
            mBadgeView.setText(badgeText);
            measureBadgeSize();
        }
    }

    private void measureBadgeSize() {
        if (isRound && !StringUtils.isNullOrEmpty(badgeText)) {
           /* TextPaint paint = mBadgeView.getPaint();
            int w = (int) (paint.measureText(badgeText) + 0.5);
            Paint.FontMetrics fontMetrics = paint.getFontMetrics();
            int h = (int) (fontMetrics.bottom - fontMetrics.top+0.5);
            int max = Math.max(w, h);*/

            AppUtils.measureView(mBadgeView);
            int measuredHeight = mBadgeView.getMeasuredHeight();
            int measuredWidth = mBadgeView.getMeasuredWidth();
            int max = Math.max(measuredHeight, measuredWidth);
            ViewGroup.LayoutParams layoutParams = mBadgeView.getLayoutParams();
            layoutParams.width = max;
            layoutParams.height = max;
            mBadgeView.setLayoutParams(layoutParams);
        }
    }


}
